<?php
/**
 *前台约车记录控制器
 */

namespace Home\Controller;

class MeetingCarRecordController extends ComController
{


    public function index(){

        $this->display();
    }
  //前台驾校约车记录列表----当选择科目三时
    public function ajax_index()
    {
        $user_id = session('user.id');

        $where['user_id'] = $user_id;

        $type = $_GET['type'];
        switch($type){
            case 'all': break;
            case '1':
            case '2':$where['course'] = $type;break;
        }
        $info = M('meeting_car_record ')
            ->join('as r  left join  driving_appointment_time  as  t   on  r.time_id = t.id  ')
            ->field('r.coach_name,r.coach_head,r.time,r.price,r.id,r.day,r.category,r.status,t.time as study_hours')
            ->where($where)
            ->order('r.id desc')
            ->select();

        foreach($info as $k => $v){
            $arr = explode('-',$v['study_hours']);
            $time = round((strtotime($arr[1]) - strtotime($arr[0])) / 3600 );
            $info[$k]['study_hours'] = $time;
        }

        $this->assign('record',$info);
        $this->display();
    }


    //前台驾校约车记录列表----当选择科目二时
    public  function  subject(){

        //获取登陆的用户信息1
        $UserId = session('user_id');
        $UserHead = session('user_head');
        //获取登陆的用户信息2
        $UserId = session('user_id');
        $UserId=1;
        $user = M('user');
        $data=$user->where('id ='.$UserId )->find();
        $head=$data['pro'];
//      dump($data);
//      exit;


        //搜索分页，加科目三
        $p= intval($_GET['p'])?$_GET['p']:1;
        $MeetingCarRecord = M('MeetingCarRecord');
        $pagesize = 2;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');

        $where = 'mcr.user_id ='. $UserId.' and mcr.course = 1' ;

        $count= $MeetingCarRecord->join('as mcr left join  driving_appointment_time  as at    on mcr.time_id=at.id')
            ->field('mcr.id, mcr.user_id, mcr.user_name, mcr.id, mcr.coach_head, mcr.coach_name,mcr.price,mcr.time, mcr.day, mcr.study_hours, mcr.category, mcr.course, mcr.status, at.time')
            ->where($where)
            ->order('mcr.id asc' )
            ->count();

        $data= $MeetingCarRecord->join('as mcr left join  driving_appointment_time  as at    on mcr.time_id=at.id')
            ->field('mcr.id, mcr.user_id, mcr.user_name, mcr.coach_name, mcr.coach_head,mcr.price,mcr.time, mcr.day, mcr.study_hours, mcr.category, mcr.course, mcr.status, at.time')
            ->where($where)
            ->order('mcr.id asc' )
            ->limit($offset . ',' . $pagesize)
            ->select();

//
//          dump($data);
//          exit;
//        dump($MeetingCarRecord->getLastSql());
//        exit;


        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->display('index');


    }
    //判断是否可退款
    public function backPrice() {
        $id = I('post.id','','intval');
        $data = M('meeting_car_record')->where(array('id'=>$id))->find();
        if($data['day']>strtotime(date('Y-m-d H:i:s'))){
            //可退款
           echo 1;exit;
        }else{
            $time = explode('-',$data['time']);
            $lastime = date("H",$time[0]);
            $nowtime = date("H");
            if($lastime-$nowtime>6){
                //可退款
                echo 1;exit;
            }else{
                //不可退款
                echo 2;exit;//距离学习时间小于6个小时不能退款
            }
        }
        
    }
    //报名记录
    public function baoming(){
        $user_id = session('user.id');
        $data = M('sign_record')
                ->field("m.name,driving_sign_record.user_name,driving_sign_record.money,driving_sign_record.add_time,driving_sign_record.status")
                ->join("driving_member as m on m.uid = driving_sign_record.d_id")
                ->where(array('driving_sign_record.uid'=>$user_id))->select();
        $this->assign('record',$data);
        $this->display();
    }







}